package com.uchengwang.ui.cm_ui.cycle.bridge;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.cycle.bean.CommentConfig;
import com.uchengwang.ui.cm_ui.cycle.bean.CommentItem;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleCmtBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleReplyBean;
import com.uchengwang.ui.self.circledemo.bean.User;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

/**
 * 
* @ClassName: CirclePresenter 
* @Description: 通知model请求服务器和通知view更新
* @author yiw
* @date 2017-05-07 下午4:06:03
 * 评论相关接口 评论 删除 回复等
*
 */
public class CirclePresenter{
	private CircleContract.CycleShareView view;
	public static final int ADD_CMT = 1;
	public static final int ADD_REPLY = 2;
	public static final int DELETE_CMT = 3;
	public static final int DELETE_REPLY = 4;
	private Activity activity;

	public CirclePresenter(CircleContract.CycleShareView view, Activity activity){
		this.view = view;
		this.activity = activity;
	}


	/**
	 * 增加社圈里的评论或者回复
	 * */
	public void addCycleCmtOrReply(final int type, MapRequest map, final CommentConfig config){
		LoadingUtils.getUtils().loading(activity);
		String url = "";
		if (type == ADD_CMT ){
			//增加评论
			url = "/cycle/share/cmt";
		}else {
			//增加对评论或者回复的回复
			url = "/cycle/share/reply";
		}

		HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.POST, url, map, new HttpCallBack() {
			@Override
			public void onError(int status, String msg) {
				LoadingUtils.getUtils().hide();
				view.addCmtOrReply(config, null);
			}

			@Override
			public void onResp(int status, String msg, String data) {
				LoadingUtils.getUtils().hide();
				if (status ==0 && !TextUtils.isEmpty(data)){
					CommentItem commentItem = new CommentItem();
					if (type == ADD_CMT ){
						//返回评论的bean
						CycleCmtBean cmtBean = JsonUtils.getManager().stringToBean(data, CycleCmtBean.class);
						commentItem.setId(cmtBean.getId());
						commentItem.setContent(cmtBean.getComment());
						commentItem.setUser(new User(cmtBean.getUid(), cmtBean.getNickName(), cmtBean.getUserAvatar()));

					}else {
						//返回回复的bean
						CycleReplyBean replyBean = JsonUtils.getManager().stringToBean(data, CycleReplyBean.class);
						commentItem.setId(replyBean.getId());
						commentItem.setCmtId(replyBean.getCmtId());
						commentItem.setReplyId(replyBean.getReplyId());
						commentItem.setContent(replyBean.getReplyContent());
						commentItem.setUser(new User(UCommonUtil.getUtil().getId(), UChengApplication.nickName, UChengApplication.userAvatar));
						commentItem.setToReplyUser(config.replyUser);
					}

					view.addCmtOrReply(config, commentItem);


				}else {
					view.addCmtOrReply(config, null);
				}

			}
		});


	}

	/**
	 * 删除社圈里的评论或者回复
	 * @param id 评论或者回复的id
	 * */
	public void deleteCycleCmtOrReply(int type, final int circlePosition, final long id){
		LoadingUtils.getUtils().loading(activity);
		String url = "";
		MapRequest map = new MapRequest();
		if (type == DELETE_CMT ){
			//删除评论
			url = "/cycle/share/cmt";
			map.put(BaseConfig.CMT_ID, id);
		}else {
			//删除对评论或者回复的回复
			url = "/cycle/share/reply";
			map.put(BaseConfig.REPLY_ID, id);
		}

		HttpManager.newInstance(activity).query(IHttpManager.RequestMethod.DELETE, url, map, new HttpCallBack() {
			@Override
			public void onError(int status, String msg) {
				LoadingUtils.getUtils().hide();
				//-1代表没有删除成功
				view.deleteCmtOrReply(-1, -1);
			}

			@Override
			public void onResp(int status, String msg, String data) {
				LoadingUtils.getUtils().hide();
				if (status ==0){
					view.deleteCmtOrReply(circlePosition, id);
				}else {
					view.deleteCmtOrReply(-1, -1);
				}
			}
		});
	}



	/**
	 *
	 * @param commentConfig
	 */
	public void showEditTextBody(CommentConfig commentConfig){
		if(view != null){
			view.editTextBodyVisible(View.VISIBLE, commentConfig);
		}
	}

    /**
     * 清除对外部对象的引用，反正内存泄露。
     */
    public void recycle(){
        this.view = null;
    }
}
